package com.yunquan.ohana.ui.calendar.lists.add

import android.app.Application
import androidx.databinding.ObservableArrayList
import androidx.databinding.ObservableField
import androidx.lifecycle.LifecycleOwner
import com.google.gson.Gson
import com.yunquan.ohana.BR
import com.yunquan.ohana.R
import com.yunquan.ohana.base.binding.command.BindingAction
import com.yunquan.ohana.base.binding.command.BindingCommand
import com.yunquan.ohana.base.bus.event.SingleLiveEvent
import com.yunquan.ohana.base.ui.BaseViewModel
import com.yunquan.ohana.entity.ListsEntity
import com.yunquan.ohana.http.BaseHttp
import com.yunquan.ohana.http.observer.BaseObserver
import com.yunquan.ohana.http.transformer.CommonTransformer
import com.yunquan.ohana.ui.calendar.profiles.add.ColorItemViewModel
import com.yunquan.ohana.utils.ToastUtils
import com.yunquan.ohana.utils.Utils
import io.reactivex.rxjava3.disposables.Disposable
import me.tatarka.bindingcollectionadapter2.ItemBinding
import okhttp3.RequestBody.Companion.toRequestBody

class ListsAddViewModel(application: Application) : BaseViewModel(application) {

    val ui = SingleLiveEvent<Int>()
    val title = ObservableField<String>()
    val isUserType = ObservableField<Boolean>()
    val btnSaveTxt = ObservableField<String>()
    val isShowDelete = ObservableField<Boolean>()
    val content = ObservableField<String>()
    val selectedColor = ObservableField<Int>()
    val isHide = ObservableField<Boolean>(false)
    val items = ObservableArrayList<ColorItemViewModel<ListsAddViewModel>>()
    val itemBinding = ItemBinding.of<ColorItemViewModel<ListsAddViewModel>>(
        BR.colorItemViewModel,
        R.layout.item_select_color,
    )

    var mDeviceId: String? = null
    var mListsEntity: ListsEntity? = null

    val onUpdateClick = BindingCommand<Any>(object : BindingAction {
        override fun call() {
            createLists()
        }
    })
    val onDeleteClick = BindingCommand<Any>(object : BindingAction {
        override fun call() {
            deleteLists()
        }
    })

    override fun onCreate(owner: LifecycleOwner) {
        super.onCreate(owner)
        items.add(ColorItemViewModel(this, Utils.instance.context.getColor(R.color.c_FF9932)))
        items.add(ColorItemViewModel(this, Utils.instance.context.getColor(R.color.c_31AEFD)))
        items.add(ColorItemViewModel(this, Utils.instance.context.getColor(R.color.c_B261FF)))
        items.add(ColorItemViewModel(this, Utils.instance.context.getColor(R.color.c_19E9A4)))
        items.add(ColorItemViewModel(this, Utils.instance.context.getColor(R.color.c_EFE231)))
        items.add(ColorItemViewModel(this, Utils.instance.context.getColor(R.color.c_FF6868)))
        items.add(ColorItemViewModel(this, Utils.instance.context.getColor(R.color.c_FF69B2)))
        items.add(ColorItemViewModel(this, Utils.instance.context.getColor(R.color.c_8AEF31)))
        items.add(ColorItemViewModel(this, Utils.instance.context.getColor(R.color.c_8C84FF)))
        items.add(ColorItemViewModel(this, Utils.instance.context.getColor(R.color.c_49E9E1)))
        if (mListsEntity != null) {
            items[mListsEntity!!.color!!.toInt()].checked.set(true)
            selectedColor.set(items[mListsEntity!!.color!!.toInt()].color.get())
        } else {
            items[0].checked.set(true)
            selectedColor.set(items[0].color.get())
        }
    }

    private fun createLists() {
        val map = mutableMapOf<String, Any?>()
        map["id"] = mListsEntity?.id
        map["deviceId"] = mDeviceId
        map["isShow"] = if (isHide.get() == true) 1 else 2
        map["name"] = content.get()
        map["color"] = items.indexOfFirst { it.checked.get() == true }.toString()
        val body = Gson().toJson(map).toRequestBody(BaseHttp.MEDIA_TYPE_JSON)
        BaseHttp.api.createLists(body)
            .compose(CommonTransformer())
            .subscribe(object : BaseObserver<List<ListsEntity>>() {
                override fun onSubscribe(d: Disposable) {
                    super.onSubscribe(d)
                    showLoading()
                }

                override fun onSuccess(result: List<ListsEntity>?) {
                    super.onSuccess(result)
                    if (result != null) {
                        if (mListsEntity != null) {
                            val id = mListsEntity!!.id
                            mListsEntity = result.first { it.id == id }
                        }
                        ui.value = 0
                    } else {
                        ToastUtils.showShort(R.string.failed_to_create_a_new_tag_group)
                    }
                }

                override fun onComplete() {
                    super.onComplete()
                    hideLoading()
                }
            })
    }

    private fun deleteLists() {
        if (mListsEntity?.id == null) {
            return
        }
        val map = mutableMapOf<String, Any?>()
        map["deviceId"] = mDeviceId
        map["listsId"] = arrayListOf(mListsEntity!!.id!!)
        val body = Gson().toJson(map).toRequestBody(BaseHttp.MEDIA_TYPE_JSON)
        BaseHttp.api.deleteListsToId(body)
            .compose(CommonTransformer())
            .subscribe(object : BaseObserver<Boolean>() {
                override fun onSubscribe(d: Disposable) {
                    super.onSubscribe(d)
                    showLoading()
                }

                override fun onSuccess(result: Boolean?) {
                    super.onSuccess(result)
                    if (result == true) {
                        mListsEntity = null
                        ui.value = 1
                    } else {
                        ToastUtils.showShort(R.string.failed_to_edit_the_tag_group)
                    }
                }

                override fun onComplete() {
                    super.onComplete()
                    hideLoading()
                }
            })
    }
}